#!/usr/bin/python3
from openpyxl import load_workbook
from openpyxl.worksheet.datavalidation import DataValidation
import os, gc
from .Base_Plugin import Base_Plugin


class Sheet_Add_Validation(Base_Plugin):
    """添加工作表数据校验插件"""

    def __init__(self, context) -> None:
        super().__init__()
        self.context = context
        self.project_config_sheet_name = "工程配置"
        self.desensitization_level_list = "={}!$I$2:$I$5".format(
            self.project_config_sheet_name
        ) # 脱敏级别
        self.yes_no = "={}!$B$2:$B$3".format(self.project_config_sheet_name) # 是否

    def run(self, workspace, source):
        workbook = load_workbook(os.path.join(workspace, source))
        _directory = workbook["目录"]

        for directory_row in _directory.rows:
            if directory_row[1].row < self.project_config["通用开始行"]:
                continue

            table_description = directory_row[2].value.strip()

            if table_description in workbook.sheetnames:
                sheet = workbook[table_description]
                desensitization_level_list = DataValidation(
                    type="list",
                    formula1=self.desensitization_level_list,
                    allow_blank=True,
                )
                not_null = DataValidation(
                    type="list",
                    formula1=self.yes_no,
                    allow_blank=True,
                )
                unique = DataValidation(
                    type="list",
                    formula1=self.yes_no,
                    allow_blank=True,
                )
                single_verify = DataValidation(
                    type="list",
                    formula1=self.yes_no,
                    allow_blank=True,
                )
                is_write_lucene_index = DataValidation(
                    type="list",
                    formula1=self.yes_no,
                    allow_blank=True,
                )
                for row in sheet.rows:
                    if row[1].row < self.project_config["列字段开始行"]:
                        continue
                    not_null.add(row[3])
                    unique.add(row[4])
                    single_verify.add(row[7])
                    desensitization_level_list.add(row[8])
                    is_write_lucene_index.add(row[9])
                sheet.add_data_validation(not_null)
                sheet.add_data_validation(unique)
                sheet.add_data_validation(single_verify)
                sheet.add_data_validation(desensitization_level_list)
                sheet.add_data_validation(is_write_lucene_index)
                self.processed += 1
        return_val = None
        if self.processed > 0:
            saved_name = r"SheetsValidation.xlsx"
            new_path = os.path.join(workspace, saved_name)
            if os.path.exists(new_path):
                os.remove(new_path)
            workbook.save(new_path)
            return_val = saved_name
        else:
            return_val = source
        del workbook
        gc.collect()
        return return_val
